package com.umeng.analytics.pro;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;
import android.util.Base64;

import com.umeng.analytics.AnalyticsConfig;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public final class aq implements au, az {
    public class a {
        private final aq a;
        private ca.h b;
        private int c;
        private int d;
        private int e;
        private int f;

        public a(aq arg3) {
            this.a = arg3;
            this.c = -1;
            this.d = -1;
            this.e = -1;
            this.f = -1;
            int[] v0 = aq.a(arg3).a(-1, -1);
            this.c = v0[0];
            this.d = v0[1];
        }

        public void a(com.umeng.analytics.pro.af.a arg3) {
            int[] v0 = arg3.a(-1, -1);
            this.c = v0[0];
            this.d = v0[1];
        }

        public void a(int arg1, int arg2) {
            this.e = arg1;
            this.f = arg2;
        }

        protected void a(boolean arg5) {
            ca.h v0_1;
            int v1 = 1;
            int v0 = 0;
            if (aq.b(this.a).d()) {
                if (!(this.b instanceof ca.b) || !this.b.a()) {
                    v1 = 0;
                }

                if (v1 != 0) {
                    v0_1 = this.b;
                } else {
                    v0_1 = new ca.b(aq.c(this.a), aq.b(this.a));
                }

                this.b = v0_1;
            } else {
                if (!(this.b instanceof ca.c) || !this.b.a()) {
                    v1 = 0;
                }

                if (v1 != 0) {
                    return;
                }

                if ((arg5) && (aq.d(this.a).a())) {
                    this.b = new ca.c(((int) aq.d(this.a).b()));
                    aq.a(this.a, ((int) aq.d(this.a).b()));
                    return;
                }

                if ((by.a) && (aq.a(this.a).b())) {
                    this.b = new com.umeng.analytics.pro.ca.a(aq.c(this.a));
                    return;
                }

                if ((aq.e(this.a).a()) && ("RPT".equals(aq.e(this.a).f()))) {
                    if (aq.e(this.a).b() == 6) {
                        if (aq.a(this.a).a()) {
                            v0 = aq.a(this.a).d(90000);
                        } else if (this.d > 0) {
                            v0 = this.d;
                        } else {
                            v0 = this.f;
                        }
                    }

                    this.b = this.b(aq.e(this.a).b(), v0);
                    return;
                }

                v1 = this.e;
                v0 = this.f;
                if (this.c != -1) {
                    v1 = this.c;
                    v0 = this.d;
                }

                this.b = this.b(v1, v0);
            }
        }

        public ca.h b(boolean arg2) {
            this.a(arg2);
            return this.b;
        }

        private ca.h b(int arg5, int arg6) {
            d v0_1;
            h v0;
            e v0_5;
            switch (arg5) {
                case 0: {
                    if ((this.b instanceof ca.g)) {
                        return this.b;
                    }
                    return new ca.g();
                }
                case 1: {
                    if ((this.b instanceof ca.d)) {
                        return this.b;
                    }
                    return new ca.d();
                }
                case 4: {
                    if ((this.b instanceof ca.f)) {
                        return this.b;
                    }
                    return new ca.f(aq.c(this.a));
                }
                case 5: {
                    if ((this.b instanceof ca.i)) {
                        return this.b;
                    }
                    return new ca.i(aq.c());
                }
                case 6: {
                    if ((this.b instanceof ca.e)) {
                        ca.e v1 = (ca.e) this.b;
                        v1.a(((long) arg6));
                        return v1;
                    }
                    return new ca.e(aq.c(this.a), ((long) arg6));
                }
                case 8: {
                    if ((this.b instanceof ca.j)) {
                        return this.b;
                    }
                    return new ca.j(aq.c(this.a));
                }
                default: {
                    if ((this.b instanceof ca.d)) {
                        return this.b;
                    }
                    return new ca.d();
                }
            }
        }
    }

    String a;
    private cc b;
    private be c;
    private bh d;
    private bg e;
    private bi f;
    private a g;
    private com.umeng.analytics.pro.af.a h;
    private long i;
    private static Context j = null;
    private int k;
    private JSONArray l;
    private final int m;
    private int n;
    private int o;
    private long p;
    private static final String q = "thtstart";
    private static final String r = "gkvc";
    private static final String s = "ekvc";
    private final long t;

    public aq(Context arg7) {
        this.b = null;
        this.c = null;
        this.d = null;
        this.e = null;
        this.f = null;
        this.g = null;
        this.h = null;
        this.i = 0;
        this.a = null;
        this.k = 10;
        this.l = new JSONArray();
        this.m = 5000;
        this.n = 0;
        this.o = 0;
        this.p = 0;
        this.t = 28800000;
        aq.j = arg7;
        this.c = new be(arg7);
        this.b = cc.a(arg7);
        this.h = af.a(arg7).b();
        this.g = new a(this);
        this.e = bg.a(aq.j);
        this.d = bh.a(aq.j);
        this.f = bi.a(aq.j, this.c);
        SharedPreferences v0 = ba.a(aq.j);
        this.p = v0.getLong("thtstart", 0);
        this.n = v0.getInt("gkvc", 0);
        this.o = v0.getInt("ekvc", 0);
        this.a = af.a(aq.j).b().b(null);
    }

    public final void a(Context arg5) {
        try {
            if (aq.j == null) {
                aq.j = arg5;
            }

            if (this.l.length() > 0) {
                w.a(aq.j).a(this.l);
                this.l = new JSONArray();
            }

            ba.a(aq.j).edit().putLong("thtstart", this.p).putInt("gkvc", this.n).putInt("ekvc", this.o).commit();
        } catch (Throwable v0) {
        }
    }

    public final void a() {
        if (bv.l(aq.j)) {
            this.d();
        } else {
            by.b("network is unavailable");
        }
    }

    static com.umeng.analytics.pro.af.a a(aq arg1) {
        return arg1.h;
    }

    private void a(int arg5) {
        this.c(this.a(new int[]{arg5, ((int) (System.currentTimeMillis() - this.c.m()))}));
        bz.a(new cb() {
            @Override
            public void a() {
                aq.this.a();
            }
        }, ((long) arg5));
    }

    protected final JSONObject a(int[] arg11) {
        JSONObject v1_7;
        SharedPreferences v3_3 = null;
        String v3_2;
        JSONObject v5_1 = null;
        SharedPreferences v1_1 = null;
        JSONObject v4 = null;
        JSONObject v3;
        JSONObject v2 = null;
        JSONObject v0 = null;
        try {
            if (TextUtils.isEmpty(AnalyticsConfig.getAppkey(aq.j))) {
                by.e("Appkey is missing ,Please check AndroidManifest.xml");
                return v0;
            }

            this.a(aq.j);
            v2 = w.a(aq.j).a();
            if (v2 == null) {
                v2 = new JSONObject();
            }
            v3 = v2.getJSONObject("body");
        } catch (Throwable v1) {
            v3 = new JSONObject();
        }
        try {
            v4 = new JSONObject(v3.toString());
            v1_1 = ba.a(aq.j);
            if (v1_1 != null) {
                String v5 = v1_1.getString("userlevel", "");
                if (!TextUtils.isEmpty(((CharSequence) v5))) {
                    v3.put("userlevel", v5);
                }
            }

            if ((this.c.f()) && this.i != 0) {
                v5_1 = new JSONObject();
                v5_1.put("ts", this.i);
                v3.put("activate_msg", v5_1);
                v4.put("activate_msg", v5_1);
            }

            v5_1 = new JSONObject();
            JSONObject v6 = com.umeng.analytics.pro.m.a(aq.j).b();
            if (v6 != null && v6.length() > 0) {
                v5_1.put("ag", v6);
            }

            v6 = com.umeng.analytics.pro.m.a(aq.j).c();
            if (v6 != null && v6.length() > 0) {
                v5_1.put("ve_meta", v6);
            }

            if (v5_1.length() > 0) {
                v3.put("cc", v5_1);
                v4.put("cc", v5_1);
            }

            String[] v5_2 = com.umeng.analytics.c.a(aq.j);
            if (v5_2 != null && !TextUtils.isEmpty(v5_2[0]) && !TextUtils.isEmpty(v5_2[1])) {
                v6 = new JSONObject();
                v6.put("provider", v5_2[0]);
                v6.put("puid", v5_2[1]);
                if (v6.length() > 0) {
                    v3.put("active_user", v6);
                    v4.put("active_user", v6);
                }
            }

            if (bg.a(aq.j).a()) {
                this.d(v3);
            }

            this.d.a(v3, aq.j);
            if (arg11 != null && arg11.length == 2) {
                v5_1 = new JSONObject();
                v6 = new JSONObject();
                v6.put("interval", arg11[0] / 1000);
                v6.put("latency", arg11[1]);
                v5_1.put("latent", v6);
                v3.put("control_policy", v5_1);
            }

            if (v3.length() <= 0) {
                try {
                    v2.remove("body");
                } catch (Throwable v3_1) {
                }
            } else {
                v2.put("body", v3);
            }
            v5_1 = new JSONObject();
            v5_1.put("appkey", AnalyticsConfig.getAppkey(aq.j));
            v5_1.put("channel", AnalyticsConfig.getChannel(aq.j));
            v3_2 = bw.a(AnalyticsConfig.getSecretKey(aq.j));
            if (!TextUtils.isEmpty(((CharSequence) v3_2))) {
                v5_1.put("secret", v3_2);
            }

            v5_1.put("display_name", bv.w(aq.j));
            v5_1.put("package_name", bv.t(aq.j));
            v5_1.put("app_signature", bv.u(aq.j));
            if (v1_1 == null) {
                v1_1 = ba.a(aq.j);
            }

            if (v1_1 != null) {
                v3_2 = v1_1.getString("vers_name", "");
                if (!TextUtils.isEmpty(((CharSequence) v3_2))) {
                    v5_1.put("app_version", v3_2);
                    v5_1.put("version_code", v1_1.getInt("vers_code", 0));
                    v3_3 = v1_1;
                } else {
                    v5_1.put("app_version", bv.b(aq.j));
                    v5_1.put("version_code", Integer.parseInt(bv.a(aq.j)));
                    v3_3 = v1_1;
                }
            } else {
                v3_3 = v1_1;
            }
        } catch (Throwable v3_1) {
            try {
                v5_1.put("app_version", bv.b(aq.j));
                v5_1.put("version_code", Integer.parseInt(bv.a(aq.j)));
                v3_3 = v1_1;
            } catch (Exception e) {

            }
        }
        try {
            if (AnalyticsConfig.mWrapperType != null && AnalyticsConfig.mWrapperVersion != null) {
                v5_1.put("wrapper_type", AnalyticsConfig.mWrapperType);
                v5_1.put("wrapper_version", AnalyticsConfig.mWrapperVersion);
            }

            v5_1.put("sdk_type", "Android");
            v5_1.put("sdk_version", AnalyticsConfig.getSDKVersion(aq.j));
            v5_1.put("vertical_type", AnalyticsConfig.getVerticalType(aq.j));
            v5_1.put("idmd5", bv.d(aq.j));
            v5_1.put("cpu", bv.a());
            v5_1.put("os", "Android");
            v5_1.put("os_version", Build.VERSION.RELEASE);
            int[] v1_2 = bv.r(aq.j);
            if (v1_2 != null) {
                v5_1.put("resolution", v1_2[1] + "*" + v1_2[0]);
            }

            v5_1.put("mc", bv.q(aq.j));
            v5_1.put("device_id", bv.c(aq.j));
            v5_1.put("device_model", Build.MODEL);
            v5_1.put("device_board", Build.BOARD);
            v5_1.put("device_brand", Build.BRAND);
            v5_1.put("device_manutime", Build.TIME);
            v5_1.put("device_manufacturer", Build.MANUFACTURER);
            v5_1.put("device_manuid", Build.ID);
            v5_1.put("device_name", Build.DEVICE);
            String v1_3 = bv.x(aq.j);
            if (!TextUtils.isEmpty(((CharSequence) v1_3))) {
                v5_1.put("sub_os_name", v1_3);
            }

            v1_3 = bv.y(aq.j);
            if (!TextUtils.isEmpty(((CharSequence) v1_3))) {
                v5_1.put("sub_os_version", v1_3);
            }

            String[] v1_4 = bv.j(aq.j);
            if ("Wi-Fi".equals(v1_4[0])) {
                v5_1.put("access", "wifi");
            } else if ("2G/3G".equals(v1_4[0])) {
                v5_1.put("access", "2G/3G");
            } else {
                v5_1.put("access", "unknow");
            }

            if (!"".equals(v1_4[1])) {
                v5_1.put("access_subtype", v1_4[1]);
            }

            v1_3 = bv.e(aq.j);
            if (!TextUtils.isEmpty(((CharSequence) v1_3))) {
                v5_1.put("mccmnc", v1_3);
            } else {
                v5_1.put("mccmnc", "");
            }

            v1_4 = bv.o(aq.j);
            v5_1.put("country", v1_4[0]);
            v5_1.put("language", v1_4[1]);
            v5_1.put("timezone", bv.m(aq.j));
            v5_1.put("carrier", bv.h(aq.j));
            try {
                v5_1.put("successful_requests", v3_3.getInt("successful_request", 0));
                v5_1.put("failed_requests", v3_3.getInt("failed_requests", 0));
                v5_1.put("req_time", v3_3.getInt("last_request_spent_ms", 0));
            } catch (Exception v1_5) {
            }
            try {
                bn v1_6 = af.a(aq.j).a();
                if (v1_6 == null) {
                } else {
                    v5_1.put("imprint", Base64.encodeToString(new cp().a(((cg) v1_6)), 0));
                }
            } catch (Exception v1_5) {
            }

            v2.put("header", v5_1);
            v4.put("sdk_version", v5_1.getString("sdk_version")).put("device_id", v5_1.getString("device_id")).put("device_model", v5_1.getString("device_model")).put("version", v5_1.getString("version_code")).put("appkey", v5_1.getString("appkey")).put("channel", v5_1.getString("channel"));
            if (!this.a(v5_1)) {
                v1_7 = v0;
            } else {
                v1_7 = v2;
            }
            if ((by.a) && v4.length() > 0) {
                by.b(String.valueOf(v4));
            }
        } catch (Throwable v1) {
            cc.a(aq.j).g();
            return v0;
        }

        if (v3_3 != null) {
            try {
                SharedPreferences.Editor v0_2 = v3_3.edit();
                v0_2.remove("vers_name");
                v0_2.remove("vers_code");
                v0_2.remove("vers_date");
                v0_2.remove("vers_pre_version");
                v0_2.commit();
            } catch (Throwable v0_1) {
            }
        }

        return v1_7;
    }

    static void a(aq arg0, int arg1) {
        arg0.b(arg1);
    }

    private void a(JSONObject arg5, String arg6, String arg7) throws JSONException {
        if (TextUtils.isEmpty(((CharSequence) arg6))) {
            arg6 = "0";
        }

        arg5.put("$pr_ve", arg6);
        if (TextUtils.isEmpty(((CharSequence) arg7))) {
            arg7 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date(System.currentTimeMillis()));
        }

        arg5.put("$ud_da", arg7);
    }

    private boolean a(boolean arg2) {
        boolean v0;
        if (!bv.l(aq.j)) {
            by.e("network is unavailable");
            v0 = false;
        } else if (this.c.f()) {
            v0 = true;
        } else {
            v0 = this.g.b(arg2).a(arg2);
        }

        return v0;
    }

    private String[] a(SharedPreferences.Editor arg7, SharedPreferences arg8, String arg9, String arg10) {
        String v2 = arg8.getString("pre_version", "");
        String v0 = arg8.getString("pre_date", "");
        String v1 = arg8.getString("cur_version", "");
        String v3 = bv.b(aq.j);
        if (!v1.equals(v3)) {
            v0 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date(System.currentTimeMillis()));
            arg7.putString("pre_version", v1);
            arg7.putString("pre_date", v0);
            arg7.putString("cur_version", v3);
            arg7.commit();
        } else {
            v1 = v2;
        }

        return new String[]{v1, v0};
    }

    public final boolean a(JSONObject arg2) {
        boolean v0 = (TextUtils.isEmpty("device_id")) || (TextUtils.isEmpty("mc")) || (TextUtils.isEmpty("resolution")) || (TextUtils.isEmpty("appkey")) || (TextUtils.isEmpty("channel")) || (TextUtils.isEmpty("app_signature")) || (TextUtils.isEmpty("package_name")) || (TextUtils.isEmpty("app_version")) ? false : true;
        return v0;
    }

    public final void a(com.umeng.analytics.pro.af.a arg3) {
        this.e.a(arg3);
        this.d.a(arg3);
        this.f.a(arg3);
        this.g.a(arg3);
        this.a = af.a(aq.j).b().b(null);
    }

    public final void a(Object arg3) {
        if (this.c.f()) {
            this.i = this.c.l();
        }

        boolean v0 = true;
        if ((arg3 instanceof JSONObject)) {
            v0 = false;
            try {
                this.b(((JSONObject) arg3));
            } catch (Throwable v1) {
            }
        }

        if (this.a(v0)) {
            this.d();
        }
    }

    private void b(int arg1) {
        this.a(arg1);
    }

    static bh b(aq arg1) {
        return arg1.d;
    }

    private void b(JSONObject arg5) {
        int v0 = 2050;
        try {
            if (v0 == arg5.getInt("__t")) {
                if (this.c(this.n)) {
                    ++this.n;
                } else {
                    return;
                }
            } else if (2049 == arg5.getInt("__t")) {
                if (this.c(this.o)) {
                    ++this.o;
                } else {
                    return;
                }
            }

            if (this.l.length() > this.k) {
                w.a(aq.j).a(this.l);
                this.l = new JSONArray();
            }

            if (this.p == 0) {
                this.p = System.currentTimeMillis();
            }

            this.l.put(arg5);
            return;
        } catch (Throwable v0_1) {
            return;
        }
    }

    public final void b() {
        this.c(this.a(new int[0]));
    }

    private void c(JSONObject arg5) {
        ad v1;
        if (arg5 != null) {
            try {
                v1 = ad.a(aq.j);
                v1.a();
                String v0_1 = Base64.encodeToString(new cp().a(v1.b()), 0);
                if (TextUtils.isEmpty(v0_1)) {

                } else {
                    JSONObject v2 = arg5.getJSONObject("header");
                    v2.put("id_tracking", v0_1);
                    arg5.put("header", v2);
                }

                byte[] v0_2 = String.valueOf(arg5).getBytes();
                if (v0_2 == null) {
                    return;
                }

                if (bt.a(aq.j, v0_2)) {
                    return;
                }

                aa v0_3 = this.e() ? aa.b(aq.j, AnalyticsConfig.getAppkey(aq.j), v0_2) : aa.a(aq.j, AnalyticsConfig.getAppkey(aq.j), v0_2);
                v0_2 = v0_3.c();
                cc v2_1 = cc.a(aq.j);
                v2_1.g();
                v2_1.a(v0_2);
                v1.d();
            } catch (Exception v0) {
            }
        }
    }

    private boolean c(int arg7) {
        boolean v0 = true;
        if (this.p != 0) {
            if (System.currentTimeMillis() - this.p > 28800000) {
                this.f();
            } else if (arg7 > 5000) {
                v0 = false;
            }
        }

        return v0;
    }

    static Context c() {
        return aq.j;
    }

    static be c(aq arg1) {
        return arg1.c;
    }

    static bi d(aq arg1) {
        return arg1.f;
    }

    private void d() {
        try {
            if (this.b.h()) {
                bc v0_1 = new bc(aq.j, this.c);
                v0_1.a(((az) this));
                if (this.d.d()) {
                    v0_1.b(true);
                }

                v0_1.a();
                return;
            }

            JSONObject v0_2 = this.a(new int[0]);
            if (v0_2.length() <= 0) {
                return;
            }

            bc v1 = new bc(aq.j, this.c);
            v1.a(((az) this));
            if (this.d.d()) {
                v1.b(true);
            }

            v1.a(v0_2);
            v1.a(this.e());
            v1.a();
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    private void d(JSONObject arg4) throws JSONException {
        JSONObject v0 = new JSONObject();
        v0.put(bg.a(aq.j).f(), bg.a(aq.j).e());
        arg4.put("group_info", v0);
    }

    private boolean e() {
        boolean v0 = false;
        switch (this.h.c(-1)) {
            case -1: {
                v0 = AnalyticsConfig.sEncrypt;
                break;
            }
            case 1: {
                v0 = true;
                break;
            }
        }

        return v0;
    }

    static bg e(aq arg1) {
        return arg1.e;
    }

    private void f() {
        this.n = 0;
        this.o = 0;
        this.p = System.currentTimeMillis();
    }
}

